package org.hotpotmaterial.code.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hotpotmaterial.code.repository.ApiObjRepository;
import org.hotpotmaterial.code.repository.LoginInfoRepository;
import org.hotpotmaterial.code.repository.ProjectRepository;
import org.hotpotmaterial.code.service.ICountDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CountDataServiceImpl implements ICountDataService {

	@Autowired
	private LoginInfoRepository loginInfoRep;

	@Autowired
	private ProjectRepository projectRep;

	@Autowired
	private ApiObjRepository apiObjRep;

	@Override
	public int getPeopleCount() {
		List<String> list = loginInfoRep.findDistinctUserId();
		if (list != null) {
			return list.size();
		}
		return 0;
	}

	@Override
	public long getProjectCount() {
		return projectRep.count();
	}

	@Override
	public long getInterfaceCount() {
		return apiObjRep.count();
	}

	@Override
	public List<Map<String, Object>> getMonthData() {
		Calendar cale = Calendar.getInstance(); 
		cale.add(Calendar.MONTH, -8); 
        int year = cale.get(Calendar.YEAR);  
        int month = cale.get(Calendar.MONTH) + 1; 
        String date=year+String.format("-%02d", month);
		Collection<?> map = projectRep.findMonthData(date);
		List<Map<String, Object>> retMaps = new ArrayList<Map<String, Object>>();
		Iterator<?> it = map.iterator();
		while (it.hasNext()) {
			HashMap<String, Object> node = new HashMap<String, Object>();
			Object[] ei = (Object[]) it.next();
			String mon = ei[0].toString();
			node.put("mon", mon);
			int count = Integer.parseInt(ei[1].toString());
			node.put("num", count);
			retMaps.add(node);
		}
		log.info(retMaps.toString());
		return retMaps;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.hotpotmaterial.code.service.ICountDataService#getUiCount()
	 */
	@Override
	public long getUiCount() {
		return projectRep.findUiCount();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.hotpotmaterial.code.service.ICountDataService#getActiveTop5Data()
	 */
	@Override
	public List<Map<String, Object>> getActiveTop5Data(String date) {
		Collection<?> map = projectRep.findActiveTop5(date+"%");
		List<Map<String, Object>> retMaps = new ArrayList<Map<String, Object>>();
		Iterator<?> it = map.iterator();
		while (it.hasNext()) {
			HashMap<String, Object> node = new HashMap<String, Object>();
			Object[] ei = (Object[]) it.next();
			String str = ei[0].toString();
			node.put("userName", str);
			int count = Integer.parseInt(ei[2].toString());
			node.put("loginCount", count);
			count = Integer.parseInt(ei[5].toString());
			node.put("partyCount", count);
			retMaps.add(node);
			str = ei[3]==null?"":ei[3].toString();
			node.put("dep", str);
			str = ei[4]==null?"":ei[4].toString();
			node.put("jobNo", str);
		}
		log.info(retMaps.toString());
		return retMaps;
	}

}
